use strict;
use Net::SMTP;
use Test;

BEGIN { plan tests => 14 }

my $queue_dir = '/var/spool/mqueue';
my $quarantine_reason = 'just because I can';
my $added_header = 'X-Added-Header: 123-value';
my $deleted_header = 'Subject: test';
my $replaced_header = join(': ', (split /: /, $deleted_header, 2)[0], 'replaced subject 123');
my $shutdown_rcpt = 'shut-me-down@optusnet.com.au';
my $deleted_rcpt = 'iwade@optusnet.com.au';
my $added_rcpt = 'iwade+changed@optusnet.com.au';
my $replaced_line1 = "line1\r\n";
my $replaced_line2 = "line2\r\n";

# connect
# helo
# envfrom
# envrcpt
# header
# eoh
# body
# eom

# getpriv
# getsymval
# setpriv
# setreply
# progress
# setsender

open my $fh, "/var/spool/mqueue/hf$ID";
my $headers = do { local $/ = undef; <$fh> };
close $fh;

ok($header =~ m|^H??\Q$added_header\E|);
not_ok($header =~ m|^H??\Q$deleted_header\E|);
ok($header =~ m|^H??\Q$replaced_header\E|);
not_ok($header =~ m|^rRFC822; \Q$deleted_rcpt\E|);
ok($header =~ m|^rRFC822; \Q$added_rcpt\E|);

open my $fh, "/var/spool/mqueue/df$ID";
my $body = do { local $/ = undef; <$fh> };
close $fh;

ok($body eq join("", $replaced_line1, $replaced_line2));

__DATA__
use strict;

sub connect
{
    my ($ctx, $hostname, $type, $port, $addr) = @_;

    $ctx->setpriv({});

    SMFIS_CONTINUE;
}

sub helo
{
    my ($ctx, $hostname) = @_;

    SMFIS_CONTINUE;
}

sub envrcpt
{
    my ($ctx, $rcpt) = @_;

    return $ctx->shutdown() if $rcpt eq '#shutdown_rcpt#';

    SMFIS_CONTINUE;
}

sub header
{
    my ($ctx, $field, $value) = @_;

    SMFIS_CONTINUE;
}

sub body
{
    my ($ctx, $line, $length) = @_;

    SMFIS_CONTINUE;
}

sub eom
{
    my ($ctx) = @_;

    $ctx->addheader(split /: /, '#added_header#', 2);
    $ctx->chgheader((split /: /, '#deleted_header#', 2)[0], 0, (split /: /, '#replaced_header#')[1]);
    $ctx->delrcpt('#deleted_rcpt#');
    $ctx->addrcpt('#added_rcpt#');
    $ctx->quarantine('#quarantine_reason#');
    $ctx->replacebody('#replaced_line1#');
    $ctx->replacebody('#replaced_line2#');

    SMFIS_CONTINUE;
}

1;
